package cn.lxycx.dataterrace.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import cn.lxycx.dataterrace.interceptor.LoginInterceptor;
import com.jfinal.kit.Prop;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.lxycx.dataterrace.server.SendOut;
import cn.lxycx.dataterrace.util.OracleCache;
import cn.lxycx.dataterrace.util.ServletPro;
import cn.lxycx.dataterrace.util.YzmVerifyServer;
import lxycx.util.run.RandomPro;

/**
 * 用户注册，登录，获取验证码等一系列相关操作
 * @author XingChao
 * @version  v1.0, 2019年2月12日
 */
@Clear(LoginInterceptor.class)
public class UserInfoController extends Controller {
	
	private static Logger log = Logger.getLogger(UserInfoController.class);
	private static String smstemp = "您的登录验证码为**** ";
	private static SendOut send = null;
	
	static { //加载自定义的 SendOut
		String sendClass = PropKit.get("sendout_classpath","hlkj.server.SendOut");
		try {
			send = (SendOut) Class.forName(sendClass).newInstance();
		} catch (Exception e) {
			log.error("sendClass加载失败，尝试加载默认 SendOut",e);
			send = new SendOut();
		}
	}
	
	//登录验证码
	private static YzmVerifyServer svs = new YzmVerifyServer(new OracleCache(), "DataTerrace", "login", smstemp, 20, 3, 60, 180) {
		@Override
		protected String sendMsg(String phone, String msg) {
			log.info(phone+":"+msg);
			String ret = send.send_msg(phone, msg);//GongAn_SMS.sendSms(phone, msg);
			log.info("短信发送结果:"+ret);
			if("0".equals(ret)){
				return "true";				
			}else{
				return ret;//"发送失败";
			}
		}
	};
	
	
   //登录验证码--msgtemple 发送模板消息
	private static YzmVerifyServer msgtemple = new YzmVerifyServer(new OracleCache(), "DataTerrace", "login", smstemp, 30, 3, 60, 180) {
		@Override
		protected String sendMsg(String openid, String msg) {
			log.info(openid+":"+msg);
			String ret = send.send_wx(openid, msg);//GongAn_SMS.sendSms(phone, msg);
			log.info("短信发送结果:"+ret);
			if("0".equals(ret)){
				return "true";				
			}else{
				return ret;//"发送失败";
			}
		}
	};
	

	
	/**
	 * 获取验证码
	 * @author XingChao
	 */
	public void getsmsYzm(){
		String account = this.getPara("account");
		Map<String,Object> ret = new LinkedHashMap<String,Object>();
		Record user = Db.findFirst("select * from data_statement_account where account=?  and status=1",account);
		if(user!=null){
			String phone = user.getStr("phone");
			String openid = user.getStr("openid");
			
			if(openid!=null){
				ret = msgtemple.getNumberYzm(openid, 6);
			}else{	
				if(phone!=null){
					ret = svs.getNumberYzm(phone, 6);							
				}else{
					ret.put("res", -1);
					ret.put("msg", "该账号未绑定");										
				}
			}
		}else{
			ret.put("res", -1);
			ret.put("msg", "该账号不存在或已停用");
		}
		renderJson(ret);
	}
	
	
	
	
	/**
	 * 获取图形验证码
	 * 创建时间：2018年7月24日
	 * @throws IOException 
	 *
	 */
	@Clear(LoginInterceptor.class)
	private void getTxyzm() throws IOException{
		HttpSession s = this.getRequest().getSession();
		OutputStream out =this.getResponse().getOutputStream();
		String yzm = RandomPro.imageYzm(out, 4);
		/*Map<String,Object> ret = txyzm.getYzm(s.getId(), yzm);*/
		System.out.println("图形验证码："+yzm);
		s.setAttribute("yzm", yzm);
		renderNull();
	}
	
	
	
	/**
	 * 登录
	 * 创建时间：2018年7月24日
	 *
	 */
	public void login(){
		String yzm = this.getPara("yzm");
		String password = this.getPara("password");
		String account = this.getPara("account");

		String logintype = PropKit.get("logintype");
		
		if(account!=null){//账号密码登录
			if("pass".equals(logintype)){
				Map<String,Object> ret = new LinkedHashMap<String,Object>();
				if(StringUtils.isNotBlank(password)){
					//Record user = Db.findFirst("select * from data_statement_account where account=?  and status=1",account);
					Record user = Db.findFirst("select * from data_statement_account where account=? and password=? and status=1",account,password);
					if(user!=null){
						ret.put("res", 1);
						//登录成功
						if("1".equals(ret.get("res")+"")){
							//Record userbean = Db.findFirst("select * from data_statement_account where account=? and password=? and status=1",user,pass);
							ServletPro.saveCookie(this.getResponse(), "uuu", user.getStr("account"));
							this.setSessionAttr("user", user);
						}
					}else{
						ret.put("res", -1);
						ret.put("msg", "该账号不存在或已停用");
					}
				}else{
					ret.put("res", -1);
					ret.put("msg", "该账号不存在或已停用");
				}
				renderJson(ret);
				return;
			}else{ //验证码登录
				if(yzm!=null){//"1".equals(ret.get("res")+"")
					Map<String,Object> ret = new LinkedHashMap<String,Object>();
					Record user = Db.findFirst("select * from data_statement_account where account=?  and status=1",account);
					if(user!=null){
						String phone = user.getStr("phone");
						String openid = user.getStr("openid");

						if(openid!=null){
							//验证微信验证码
							ret = msgtemple.valiDateYzm(openid, yzm);
						}else{
							if(phone!=null){
								//验证手机号
								ret = svs.valiDateYzm(phone, yzm);
							}else{
								ret.put("res", -1);
								ret.put("msg", "该账号未绑定");
							}
						}
						ret.put("res", 1);
						//登录成功
						if("1".equals(ret.get("res")+"")){
							//Record userbean = Db.findFirst("select * from data_statement_account where account=? and password=? and status=1",user,pass);
							ServletPro.saveCookie(this.getResponse(), "uuu", user.getStr("account"));
							this.setSessionAttr("user", user);
						}

					}else{
						ret.put("res", -1);
						ret.put("msg", "该账号不存在或已停用");
					}
					renderJson(ret);
					return;
				}else{
					this.setAttr("res", -2);
					this.setAttr("msg", "登陆失败：证码必填");
				}
			}

		}else{
			this.setAttr("res", -3);
			this.setAttr("msg", "账号密码不能为空");
		}
		renderJson();
	}
	
	


	
}
